package cn.skycity.common.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.skycity.common.service.ISysDeptService;
import cn.skycity.framework.core.model.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.skycity.framework.uaa.pojo.entity.SysDept;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Resource
    private ISysDeptService deptService;


    //    @ApiOperation(value = "部门表格（Table）层级列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "name", value = "部门名称", paramType = "query", dataType = "String"),
//            @ApiImplicitParam(name = "status", value = "部门状态", paramType = "query", dataType = "Long"),
//    })
    @GetMapping("/list")
    public Result<List<SysDept>> getTableList(SysDept sysDept) {
        return Result.success(deptService.list(new LambdaQueryWrapper<SysDept>()
            .eq(sysDept.getStatus()!=null,SysDept::getStatus,sysDept.getStatus())
            .like(StrUtil.isNotEmpty(sysDept.getDeptName()),SysDept::getDeptName,sysDept.getDeptName())
            .orderByAsc(SysDept::getSort)
        ));
    }


    @GetMapping("/list/exclude/{deptId}")
    public Result<List<SysDept>> excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
        List<SysDept> depts = deptService.list();
        depts.removeIf(d -> d.getId().equals(deptId)
                || StrUtil.split(d.getAncestors(),",").contains(Convert.toStr(deptId)));
        return Result.success(depts);
    }


//    @GetMapping("/select")
//    public Result<List<TreeSelectVO>> getSelectList() {
//        List<TreeSelectVO> deptSelectList = deptService.listTreeSelect();
//        return Result.success(deptSelectList);
//    }

////    @ApiOperation(value = "部门详情")
////    @ApiImplicitParam(name = "id", value = "部门id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public Result<SysDept> detail(@PathVariable Long id) {
        SysDept sysDept = deptService.getById(id);
        return Result.success(sysDept);
    }
//
////    @ApiOperation(value = "新增部门")
    @PostMapping
    public Result<Long> add(@RequestBody SysDept dept) {
        Long id = deptService.saveDept(dept);
        return Result.success(id);
    }

////    @ApiOperation(value = "修改部门")
    @PutMapping(value = "/{id}")
    public Result<Long> update(@PathVariable Long id, @RequestBody SysDept dept) {
        dept.setId(id);
        Long deptId = deptService.saveDept(dept);
        return Result.success(deptId);
    }

////    @ApiOperation(value = "删除部门")
////    @ApiImplicitParam(name = "ids", value = "部门ID，多个以英文逗号,分割拼接", required = true, paramType = "query", dataType = "String")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") String ids) {
        boolean status= deptService.deleteByIds(ids);
        return Result.judge(status);
    }

}
